from PyQt5.QtWidgets import QWidget
from PyQt5.QtCore import Qt,QThread, pyqtSignal,pyqtSlot
from qfluentwidgets import InfoBarPosition, InfoBar
from ui.upload_ticket_ui import Ui_upload_ticket
from src import get_ticket_code,add_worktime,reupload_ticket
from src.db_utils import *
from threading import Thread

class upload_ticket_cls(Ui_upload_ticket,QWidget):

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.setupUi(self)
        self.user_data = []
        self.get_id_code.clicked.connect(self.def_get_id_code)
        self.push_id_code.clicked.connect(self.def_push_id_code)
        self.add_ticket_content.clicked.connect(self.def_add_ticket_content)
        self.reupload_ticket_content.clicked.connect(self.def_reupload_ticket_content)
        self.get_ticket_content.clicked.connect(lambda: self.get_ticket("get_ticket_content"))
        self.get_ticket_content25.clicked.connect(lambda: self.get_ticket("get_ticket_content25"))
        self.get_ticket_content50.clicked.connect(lambda: self.get_ticket("get_ticket_content50"))
        self.get_ticket_content100.clicked.connect(lambda: self.get_ticket("get_ticket_content100"))
        # self.get_ticket_content1000.clicked.connect(lambda: self.get_ticket("get_ticket_content1000"))
        self.add_worktime.clicked.connect(lambda: self.def_add_worktime("add_worktime"))
        self.add_tiket_time.clicked.connect(lambda: self.def_add_worktime("add_tikettime"))
        # self.add_project_time.clicked.connect(lambda: self.get_ticket("add_projecttime"))

    def def_get_id_code(self):
        InfoBar.info(title='信息',content="获取验证码",orient=Qt.Horizontal,isClosable=True,position=InfoBarPosition.TOP_RIGHT,duration=20000,parent=self)
        db = Database()
        initialize_webdriver()
        driver = get_webdriver()
        my_account = db.get_default_set_data('my_account')
        my_passwd = db.get_default_set_data('my_passwd')
        driver.find_element(By.XPATH,'//input[@name="username"]').send_keys(my_account)     # 用户名
        driver.find_element(By.XPATH,'//input[@name="password"]').send_keys(my_passwd)      # 密码
        driver.find_element(By.XPATH,'//*[@id="btnYzm"]').click()                                               # 获取验证码
    
    def def_push_id_code(self):
        InfoBar.info(title='信息',content="登陆中，观察到浏览器登录完成后再上传",orient=Qt.Horizontal,isClosable=True,position=InfoBarPosition.TOP_RIGHT,duration=20000,parent=self)
        driver = get_webdriver()
        id_code = self.id_code.text()
        code_input = driver.find_element(By.XPATH, '//input[@name="captcha"]')
        code_input.send_keys(id_code)
        driver.find_element(By.XPATH,'//button[@class="s-input s-btn login-btn"]').click()
        # self.id_label.setVisible(False)
        self.id_code.setVisible(False)
        self.get_id_code.setVisible(False)
        self.push_id_code.setVisible(False)

    def get_ticket(self, button_id):
        InfoBar.info(title='信息',content="开始获取工单信息",orient=Qt.Horizontal,isClosable=True,position=InfoBarPosition.TOP_RIGHT,duration=20000,parent=self)
        # 创建新线程并启动，注意去掉函数后的()，通过args传递参数
        self.thread = Thread(target=get_ticket_code.get_ticket, args=(button_id,))
        self.thread.start()
        
    def def_add_worktime(self, button_id):
        InfoBar.info(title='信息',content="开始上传工时",orient=Qt.Horizontal,isClosable=True,position=InfoBarPosition.TOP_RIGHT,duration=20000,parent=self)
        self.thread = Thread(target=add_worktime.add_work_time, args=(button_id,))
        # self.thread = Thread(target=add_worktime.add_work_time)
        self.thread.start()

    def def_reupload_ticket_content(self):
        InfoBar.info(title='信息',content="开始重传工单",orient=Qt.Horizontal,isClosable=True,position=InfoBarPosition.TOP_RIGHT,duration=20000,parent=self)
        self.thread = Thread(target=reupload_ticket.reupload_ticket_content)
        self.thread.start()
        InfoBar.info(title='信息',content="重传完成",orient=Qt.Horizontal,isClosable=True,position=InfoBarPosition.TOP_RIGHT,duration=20000,parent=self)

    def def_add_ticket_content(self):
        # 创建一个新线程来初始化WebDriver
        InfoBar.info(title='信息',content="开始上传工单",orient=Qt.Horizontal,isClosable=True,position=InfoBarPosition.TOP_RIGHT,duration=20000,parent=self)
        self.thread = Thread(target=self.def_add_ticket_content_thread)
        self.thread.start()

    def def_add_ticket_content_thread(self):
        driver = get_webdriver()
        driver.switch_to.default_content()
        wait = WebDriverWait(driver, 50)

        db = Database()
        c = db.get_connection()
        default_area = db.get_default_set_data('default_area')
        
        # ticket_status=0：未上传，1：已上传工单，2：保存工时，3：提交验证，4：确认
        ticket_query = ("SELECT id, custom_name, custom_phone, serial_num,"
                "custom_company, custom_department,"
                "custom_default_area, ticket_title,"
                "ticket_code, question_ask, question_done,"
                "use_time, start_time, end_time, is_import, "
                "is_local, ticket_status, service_level, contract_id, "
                "product_id, l2_name, ticket_area, ticket_classify,ticket_phase, "
                "column_status,is_update,will_l2,ticket_saller,is_centos, "
                "local_start_time, local_end_time, local_service_time, local_content, "
                "local_service_area_detal,local_ticket_area,ticket_quality "
                " FROM ticket WHERE (is_worktime IS NULL OR is_worktime = '否') and (ticket_status IS NULL OR ticket_status = 0 OR ticket_status = '')")

        c.execute(ticket_query)
        ticket_sheet = c.fetchone()

        while ticket_sheet is not None:
            ticket_id = ticket_sheet[0]
            custom_name = ticket_sheet[1]
            custom_phone = ticket_sheet[2]
            serial_num = ticket_sheet[3]
            custom_company = ticket_sheet[4]
            custom_department = ticket_sheet[5]
            custom_default_area = ticket_sheet[6]
            ticket_title = ticket_sheet[7]
            ticket_code = ticket_sheet[8]
            question_ask = ticket_sheet[9]
            question_done = ticket_sheet[10]
            use_time = ticket_sheet[11]
            start_time = ticket_sheet[12]
            end_time = ticket_sheet[13]
            is_import = ticket_sheet[14]
            is_local = ticket_sheet[15]
            ticket_status = ticket_sheet[16]
            service_level = ticket_sheet[17]
            contract_id = ticket_sheet[18]
            product_id = ticket_sheet[19]
            l2_name = ticket_sheet[20]
            ticket_area = ticket_sheet[21]
            ticket_classify = ticket_sheet[22]
            ticket_phase = ticket_sheet[23]
            column_status = ticket_sheet[24]
            is_update = ticket_sheet[25]
            will_l2 = ticket_sheet[26]
            ticket_saller = ticket_sheet[27]
            is_centos = ticket_sheet[28]
            local_start_time =ticket_sheet[29]
            local_end_time=ticket_sheet[30]
            local_service_time=ticket_sheet[31]
            local_content=ticket_sheet[32]
            local_service_area_detal=ticket_sheet[33]
            local_ticket_area=ticket_sheet[34]
            ticket_quality =ticket_sheet[35]


            ticket_classify_query = f"SELECT select_1, select_2 FROM ticket_classify where id = {ticket_classify}"
            c.execute(ticket_classify_query)
            ticket_classify_sheet = c.fetchone()
            select_1 = ticket_classify_sheet[0]
            select_2 = ticket_classify_sheet[1]
            
            local_ticket_area_query = f"SELECT province, city, district FROM ticket_area where id = {local_ticket_area}"
            c.execute(local_ticket_area_query)
            local_ticket_area_sheet = c.fetchone()
            local_ticket_province = local_ticket_area_sheet[0]
            local_ticket_city = local_ticket_area_sheet[1]
            local_ticket_district = local_ticket_area_sheet[2]

            # 新建工单
            creat_new_ticket = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="nav_addtion"]/ul/li/span[1]')))
            creat_new_ticket.click()
            time.sleep(8)
            wait.until(EC.element_to_be_clickable((By.XPATH,'//span[@class="work-older-Close"]'))).click()
            time.sleep(1)
            creat_new_ticket.click()
            time.sleep(8)

            # 进入iframe2
            iframe2 = wait.until(EC.visibility_of_element_located((By.XPATH,'/html/body/div[3]/div[1]/div[4]/iframe[2]')))
            driver.switch_to.frame(iframe2)
            time.sleep(2)
            
            # 添加标题
            wait.until(EC.visibility_of_element_located((By.XPATH,'/html/body/div[1]/div/div[1]/div[2]/div[1]/div[2]/input'))).send_keys(ticket_title)
            time.sleep(2)

            # 选择区域
            # wait.until(EC.element_to_be_clickable((By.XPATH,'//select[@name="eavColumn137843"]/following-sibling::div'))).click()
            # xpath_default_area = f'//select[@name="eavColumn137843"]/following-sibling::div/div/ul/li[contains(text(), "{default_area}")]'
            # wait.until(EC.element_to_be_clickable((By.XPATH, xpath_default_area))).click()
            # time.sleep(1)

            # 选择服务级别
            wait.until(EC.element_to_be_clickable((By.XPATH,'//select[@name="eavColumn6069"]/following-sibling::div'))).click()
            xpath_service_level = f'//select[@name="eavColumn6069"]/following-sibling::div/div/ul/li[contains(text(), "{service_level}")]'
            wait.until(EC.element_to_be_clickable((By.XPATH, xpath_service_level))).click()
            time.sleep(2)

            # 选择服务方式
            if is_local !="远程支持":
                wait.until(EC.element_to_be_clickable((By.XPATH,'//select[@name="eavColumn1827"]/following-sibling::div'))).click()
                xpath_is_local_status = f'//select[@name="eavColumn1827"]/following-sibling::div/div/ul/li[contains(text(), "{is_local}")]'
                wait.until(EC.element_to_be_clickable((By.XPATH, xpath_is_local_status))).click()
                time.sleep(2)
            else:
                print ("默认远程单，跳过")
            
            # 选择是否更新引起
            if is_update !="否":
                wait.until(EC.element_to_be_clickable((By.XPATH,'//select[@name="eavColumn8057203"]/following-sibling::div'))).click()
                xpath_service_level = f'//select[@name="eavColumn8057203"]/following-sibling::div/div/ul/li[contains(text(), "{is_update}")]'
                wait.until(EC.element_to_be_clickable((By.XPATH, xpath_service_level))).click()
                time.sleep(2)
            else:
                print ("默认不是更新引起，跳过")
            
            # 选择服务阶段
            wait.until(EC.element_to_be_clickable((By.XPATH,'//select[@name="eavColumn180015"]/following-sibling::div'))).click()
            xpath_ticket_phase = f'//select[@name="eavColumn180015"]/following-sibling::div/div/ul/li[contains(text(),"{ticket_phase}")]'
            wait.until(EC.element_to_be_clickable((By.XPATH, xpath_ticket_phase))).click()
            time.sleep(2)

            # 点击服务序列号
            if ticket_phase == "售后支持":
                wait.until(EC.element_to_be_clickable((By.XPATH,'//input[@name="eavColumn5423"]'))).click()
            elif ticket_phase == "售前支持":
                wait.until(EC.element_to_be_clickable((By.XPATH,'//input[@name="eavColumn19961"]'))).click()
            elif ticket_phase == "项目支持":
                wait.until(EC.element_to_be_clickable((By.XPATH,'//input[@name="eavColumn19962"]'))).click()
            time.sleep(5)
            
            # 进入服务序列号列
            driver.switch_to.frame(wait.until(EC.visibility_of_element_located((By.ID,'textfield_iframe'))))
            sel_service=Select(wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'select[name="fieldName"]'))))
            if ticket_phase == "售后支持":
                sel_service.select_by_value("fwxlh")
            elif ticket_phase == "售前支持":
                sel_service.select_by_value("xmbm")
            elif ticket_phase == "项目支持":
                sel_service.select_by_value("field_1")
            time.sleep(2)

            # 添加序列号并回车
            service_value=wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[name="fieldValue"]')))
            service_value.send_keys(serial_num)
            time.sleep(2)
            service_value.send_keys(Keys.ENTER)
            time.sleep(5)

            # 点击序列号对应列并确定
            wait.until(EC.element_to_be_clickable((By.XPATH,"/html/body/div[2]/div[1]/div[2]/div[2]/table/tbody/tr"))).click()
            time.sleep(5)
            wait.until(EC.element_to_be_clickable((By.ID,"save"))).click()
            time.sleep(2)

            driver.switch_to.default_content()
            driver.switch_to.frame(iframe2)

            # 添加问题需求描述
            question_ask_value=wait.until(EC.visibility_of_element_located((By.XPATH,'//textarea[@name="eavColumn1845"]')))
            question_ask_value.send_keys(question_ask)
            time.sleep(2)

            # 工单问题分类
            wait.until(EC.element_to_be_clickable((By.XPATH,"//li[contains(@field_name, '工单问题分类')]/div[2]/p/div[1]/div[1]"))).click()
            xpath_ticket_classify1= f"//li[contains(@field_name, '工单问题分类')]/div[2]/p/div/div[1]/div[2]/div[2]/div[contains(text(), '{select_1}')]"
            xpath_ticket_classify2= f"//li[contains(@field_name, '工单问题分类')]/div[2]/p/div/div[2]/div[2]/div[2]/div[contains(text(), '{select_2}')]"
            wait.until(EC.element_to_be_clickable((By.XPATH, xpath_ticket_classify1))).click()
            wait.until(EC.element_to_be_clickable((By.XPATH,"//li[contains(@field_name, '工单问题分类')]/div[2]/p/div[1]/div[2]"))).click()
            wait.until(EC.element_to_be_clickable((By.XPATH, xpath_ticket_classify2))).click()
            time.sleep(2)

            # 公司查找
            # wait.until(EC.visibility_of_element_located((By.XPATH,'//select[@name="userId"]/following-sibling::span'))).click()
            custom_company='融信交泰'
            company_xpath = f'//ul[@class="ul_first_last  companyli"]/li[1]/div[2]/span[1]/span[1]/span[1]/span[1]'
            element = driver.find_element(By.XPATH, company_xpath)
            element_text = element.text
            time.sleep(2)

            # 判断是否包含数据
            if element_text == '-':
                print("没有公司")
                wait.until(EC.element_to_be_clickable((By.XPATH,'//ul[@class="ul_first_last  companyli"]/li/div[2]'))).click()
                time.sleep(1)
                wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@class="select2-search__field"]'))).send_keys(custom_company)
                time.sleep(2)
                wait.until(EC.element_to_be_clickable((By.XPATH,'//li[@class="select2-results__option select2-results__option--highlighted"]'))).click()
                time.sleep(1)
            else:
                print("有公司: ", element_text)
                pass
                        

            # 添加联系人
            wait.until(EC.visibility_of_element_located((By.XPATH,'//select[@id="custUserId"]/following-sibling::span'))).click()
            time.sleep(2)
            wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@class="select2-search__field"]'))).send_keys(custom_phone)
            time.sleep(4)
            # content = wait.until(EC.visibility_of_element_located((By.XPATH,'//span[@class="select2-results"]/ul/li[contains(text(), "{}")]'.format(custom))))
            # content_element = wait.until(EC.visibility_of_element_located((By.XPATH,'//span[@class="select2-results"]/ul/li[contains(text(), "{custom}")]')))
            
            content_element_found = False
            custom_xpath = f'//span[@class="select2-results"]/ul/li[contains(text(), "{custom_phone}")]'

            try:
                content_element = WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.XPATH, custom_xpath)))
                time.sleep(1)
                content_element_found = True
            except TimeoutException:
                pass

            if content_element_found:
                content_element.click()
            else:
                # wait.until(EC.element_to_be_clickable((By.XPATH,'//ul[@class="ul_first_last contactorli"]/li/div/div/a'))).click()
                link = wait.until(EC.element_to_be_clickable((By.XPATH,'//ul[@class="ul_first_last contactorli"]/li/div/div/a')))
                driver.execute_script("arguments[0].click();", link)
                wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@name="realName"]'))).send_keys(custom_name)
                time.sleep(1)
                wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@name="mobile"]'))).send_keys(custom_phone)
                time.sleep(1)
                wait.until(EC.element_to_be_clickable((By.XPATH,'//input[@name="addcontact"]'))).click()
                time.sleep(1)
                try:
                    content_element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, custom_xpath)))
                    content_element.click()
                except TimeoutException:
                    pass
            time.sleep(1)
            if is_local =='远程支持':
                # 点击升级服务台
                wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="submit_macro"]/a[1]'))).click()
                c.execute("UPDATE ticket SET ticket_status = ? WHERE id = ?", (1, ticket_id))
                db.commit_database()
                time.sleep(30)

                # 添加工时
                # ticket_time=wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@name="eavColumn33707"]')))
                # ticket_time.send_keys(start_time)
                # ticket_time=wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@name="eavColumn33708"]')))
                # ticket_time.send_keys(end_time)
                # ticket_time=wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@name="eavColumn46830"]')))
                # ticket_time.send_keys(use_time)
                # 添加工作内容
                # work_value=wait.until(EC.visibility_of_element_located((By.XPATH,'//textarea[@name="eavColumn33713"]')))
                question_final = question_ask + "---" + question_done
                # work_value.send_keys(question_final)
                # time.sleep(2)
                # 保存
                # wait.until(EC.visibility_of_element_located((By.XPATH,'//*[@id="submit_macro"]/a[1]')))
                # wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="submit_macro"]/a[1]'))).click()
                # c.execute("UPDATE ticket SET ticket_status = ? WHERE id = ?", (2, ticket_id))
                # db.commit_database()
                # time.sleep(20)

                # 一线处理记录
                question_review=wait.until(EC.visibility_of_element_located((By.XPATH,'//textarea[@name="eavColumn1824"]')))
                question_review.send_keys(question_final)
                question_done_value=wait.until(EC.visibility_of_element_located((By.XPATH,'//textarea[@name="eavColumn178979"]')))
                question_done_value.send_keys(question_final)

                
                if will_l2 =='是':
                    time.sleep(15)
                    element=wait.until(EC.visibility_of_element_located((By.XPATH,'//*[@id="submit_macro"]/a[1]')))
                    wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="submit_macro"]/a[1]'))).click()
                    c.execute("UPDATE ticket SET ticket_status = ? WHERE id = ?", (4, ticket_id))
                    c.execute("UPDATE ticket SET is_import = ? WHERE id = ?", (1, ticket_id))
                    db.commit_database()
                    # time.sleep(3)
                    # wait.until(EC.element_to_be_clickable((By.XPATH,'//button[@class="btn btn-primary modal_btn_ok"]'))).click()
                    time.sleep(3)

                else:
                    # 完成服务单
                    time.sleep(15)
                    element=wait.until(EC.visibility_of_element_located((By.XPATH,'//*[@id="submit_macro"]/a[4]')))
                    wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="submit_macro"]/a[4]'))).click()
                    c.execute("UPDATE ticket SET ticket_status = ? WHERE id = ?", (3, ticket_id))
                    db.commit_database()
                    time.sleep(3)
                    wait.until(EC.element_to_be_clickable((By.XPATH,'//button[@class="btn btn-primary modal_btn_ok"]'))).click()
                    time.sleep(3)
                    
                    # 问题验证结论
                    # question_veryfy_value=wait.until(EC.visibility_of_element_located((By.XPATH,'//textarea[@name="eavColumn171258"]')))
                    # question_veryfy_value.send_keys(question_final)
                    # # 验证通过
                    # time.sleep(10)
                    # element=wait.until(EC.visibility_of_element_located((By.XPATH,'//*[@id="submit_macro"]/a[1]')))
                    # wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="submit_macro"]/a[1]'))).click()
                    c.execute("UPDATE ticket SET ticket_status = ? WHERE id = ?", (4, ticket_id))
                    db.commit_database()
                    time.sleep(5)

            elif is_local =='现场支持':
                # 添加时间
                start_time_value=wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@name="eavColumn180378"]')))
                start_time_value.send_keys(local_start_time)
                end_time_value=wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@name="eavColumn180380"]')))
                end_time_value.send_keys(local_end_time)
                use_time_value=wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@name="eavColumn180379"]')))
                use_time_value.send_keys(local_service_time)
                
                # 点击是否联系人
                wait.until(EC.element_to_be_clickable((By.XPATH,'//select[@name="eavColumn180382"]/following-sibling::div'))).click()
                xpath_local_content = f'//select[@name="eavColumn180382"]/following-sibling::div/div/ul/li[contains(text(), "{local_content}")]'
                wait.until(EC.element_to_be_clickable((By.XPATH, xpath_local_content))).click()
                time.sleep(2)
                
                # 服务省市区
                # 点击省//li[@field_name = '座机号']/following-sibling::li[1]
                wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@field_name = '座机号']/following-sibling::li[1]/div[2]/p/div[1]/div[1]"))).click()
                wait.until(EC.visibility_of_element_located((By.XPATH,"//li[@field_name = '座机号']/following-sibling::li[1]/div[2]/p/div[1]/div[1]/div[2]/div[1]/input"))).send_keys(local_ticket_province)
                # xpath_local_ticket_province= f"//li[@field_name = '座机号']/following-sibling::li[1]/div[2]/p/div/div[1]/div[2]/div[1]/div[contains(text(), '{local_ticket_province}')]"
                wait.until(EC.element_to_be_clickable((By.XPATH, "//li[@field_name = '座机号']/following-sibling::li[1]/div[2]/p/div[1]/div[1]/div[2]/div[3]/div"))).click()
                
                # 点击市
                wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@field_name = '座机号']/following-sibling::li[1]/div[2]/p/div[1]/div[2]"))).click()
                wait.until(EC.visibility_of_element_located((By.XPATH,"//li[@field_name = '座机号']/following-sibling::li[1]/div[2]/p/div[1]/div[2]/div[2]/div[1]/input"))).send_keys(local_ticket_city)
                # xpath_local_ticket_city= f"//li[@field_name = '座机号']/following-sibling::li[1]/div[2]/p/div/div[2]/div[2]/div[2]/div[contains(text(), '{local_ticket_city}')]"
                wait.until(EC.element_to_be_clickable((By.XPATH, "//li[@field_name = '座机号']/following-sibling::li[1]/div[2]/p/div[1]/div[2]/div[2]/div[3]/div"))).click()

                # 点击区
                wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@field_name = '座机号']/following-sibling::li[1]/div[2]/p/div[1]/div[3]"))).click()
                wait.until(EC.visibility_of_element_located((By.XPATH,"//li[@field_name = '座机号']/following-sibling::li[1]/div[2]/p/div[1]/div[3]/div[2]/div[1]/input"))).send_keys(local_ticket_district)
                # xpath_local_ticket_district= f"//li[@field_name = '座机号']/following-sibling::li[1]/div[2]/p/div/div[2]/div[2]/div[2]/div[contains(text(), '{local_ticket_district}')]"
                wait.until(EC.element_to_be_clickable((By.XPATH, "//li[@field_name = '座机号']/following-sibling::li[1]/div[2]/p/div[1]/div[3]/div[2]/div[3]/div"))).click()
                time.sleep(2)
                
                # 服务详细地点
                xpath_local_service_area_detal=wait.until(EC.visibility_of_element_located((By.XPATH,'//textarea[@name="eavColumn8001365"]')))
                xpath_local_service_area_detal.send_keys(local_service_area_detal)
                time.sleep(2)

                # 点击升级服务台
                wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="submit_macro"]/a[1]'))).click()
                c.execute("UPDATE ticket SET ticket_status = ? WHERE id = ?", (4, ticket_id))
                db.commit_database()
                time.sleep(30)

            driver.switch_to.default_content()
            wait.until(EC.element_to_be_clickable((By.XPATH,'//span[@class="work-older-Close"]'))).click()
            time.sleep(1)
            print("ok_close")
            
            db.close_connection()
            db = Database()
            c = db.get_connection()
            c.execute(ticket_query)
            ticket_sheet = c.fetchone()

